//
//  OYStatusViewModel.swift
//  weiboCopycat
//
//  Created by Orange Yu on 2016/12/4.
//  Copyright © 2016年 Orange Yu. All rights reserved.
//

import UIKit

class OYStatusViewModel: NSObject {

    var statusModel: OYStatusModel? {
        didSet {
            handleVarifedImage(verifiedType: statusModel?.user?.verified_type ?? 0)
            handleMbRankImage(mbrank: statusModel?.user?.mbrank ?? 0)
            repostTitle = handleStatusCount(statusCount: statusModel?.reposts_count ?? 0, initialTitle: "转发")
            commentTitle = handleStatusCount(statusCount: statusModel?.comments_count ?? 0, initialTitle: "评论")
            likeTitle = handleStatusCount(statusCount: statusModel?.attitudes_count ?? 0, initialTitle: "赞")
            handleRetweetContent()
            handleContetnSource()
        }
    }
    
    /// 认证图片
    var verifiedImage: UIImage?
    /// 会员等级图片
    var mbRankImage: UIImage?
    /// 转发数
    var repostTitle: String?
    /// 评论数
    var commentTitle: String?
    /// 点赞数
    var likeTitle: String?
    /// 转发微博的内容
    var retweetContent:String?
    /// 微博来源
    var contentSource:String?
    /// 微博发布时间
    var composeTime:String? {
        
        guard let createDate = statusModel?.created_at else { return nil}
        
 
        return createDate.weiboConposeDate
        
        
    }
    
    private func isThisYear (createTime:Date)->(Bool) {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "YYYY"
        
        let createYear = dateFormatter.string(from: createTime)
        let currentYear = dateFormatter.string(from: Date())
        
        return createYear == currentYear
        
    }
    
    /// 处理微博来源的内容
    func handleContetnSource () {
        // <a href="http://app.weibo.com/t/feed/6vtZb0" rel="nofollow">微博 weibo.com</a>
        // 拿到来源的字符串
        if let source = statusModel?.source {
            // 进行截取
            if source.contains("\">") && source.contains("</") {
                if let startRange = source.range(of: "\">"),let endRange = source.range(of: "</") {
                    // 找到截取字符串的起点和重点坐标
                    let startIndex = startRange.upperBound
                    let endIndex = endRange.lowerBound
                    
                    contentSource = "来自" + source.substring(with: startIndex ..< endIndex)
                }
                
            }
        }
        
        
        // 赋值
        
    }

    /// 处理转发微博内容
    func handleRetweetContent () {
        if statusModel?.retweeted_status != nil {
            if let name = statusModel?.retweeted_status?.user?.screen_name,let content = statusModel?.retweeted_status?.text {
                retweetContent = "@\(name):" + content
            }
        }

        
    }

    /// toolBar数据的处理
    ///
    /// - Parameters:
    ///   - statusCount: 评论/转发/点赞的数量
    ///   - initialTitle: 当没有数字时候的默认文字
    /// - Returns: 返回需要显示的数字
    func handleStatusCount(statusCount:Int,initialTitle:String) -> String {
        if statusCount > 0 && statusCount < 10000 {
          return "\(statusCount)"
        }else if statusCount > 10000 {
            let thoundsCount = CGFloat(statusCount / 1000) / 10
            var thoundsStr = "\(thoundsCount)万"
            if thoundsStr.contains(".0") {
               thoundsStr = thoundsStr.replacingOccurrences(of: ".0", with: "")
            }
            return thoundsStr
        }else {
            return initialTitle
        }
    }
    
    /// 对认证图片的处理
    ///
    /// - Parameter verifiedType: 认证类型
    func handleVarifedImage(verifiedType:Int) {
        switch verifiedType {
        case 1:
            verifiedImage = UIImage(named: "avatar_vip")
        case 2,3,5:
            verifiedImage = UIImage(named: "avatar_enterprise_vip")
        case 220:
            verifiedImage = UIImage(named: "avatar_grassroot")
        default:
            verifiedImage = nil
        }
    }
    
    /// 对会员等级的处理
    ///
    /// - Parameter mbrank: 会员等级
    func handleMbRankImage(mbrank: Int) {
        if mbrank >= 1 && mbrank <= 6 {
            self.mbRankImage = UIImage(named: "common_icon_membership_level\(mbrank)")
        }else {
            self.mbRankImage = nil
        }
    }
    
    
}
